package Dao;

import Entity.Room;
import Mybatis.Mybatis;
import org.apache.ibatis.session.SqlSession;
import java.util.List;

public class RoomDao implements RoomDaoImpl {
    public List<Room> findAllRoom() {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.RoomMapper.findAllRoom";
        List<Room> rooms = session.selectList(statement);
        session.close();
        return rooms;
    }
    public Room findByRoomid(String roomid) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.RoomMapper.findByRoomid";
        Room room = session.selectOne(statement, roomid);
        session.close();
        return room;
    }
    public List<Room> findByRoomtype(int roomtype) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.RoomMapper.findByRoomtype";
        List<Room> rooms = session.selectList(statement, roomtype);
        session.close();
        return rooms;
    }
    public boolean addRoom(Room room) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.RoomMapper.addRoom";
        int count = session.insert(statement, room);
        session.commit();
        session.close();
        if(count == 0) {
            return false;
        } else {
            return true;
        }
    }
    public boolean updateRoom(Room room) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.RoomMapper.updateRoom";
        int count = session.update(statement, room);
        session.commit();
        session.close();
        if(count == 0) {
            return false;
        } else {
            return true;
        }
    }
    public boolean deleteRoom(Room room) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.RoomMapper.deleteRoom";
        int count = session.delete(statement, room);
        session.commit();
        session.close();
        if(count == 0) {
            return false;
        } else {
            return true;
        }
    }

    // ����
    public static void main(String[]args) {
        RoomDao test = new RoomDao();

        Room room = new Room("1016", 1, 30);
        test.addRoom(room);
        room.setTotal(100);
        test.updateRoom(room);
        test.deleteRoom(room);
        List<Room> rooms = test.findAllRoom();
        for(int i = 0; i < rooms.size(); i++) {
            System.out.println(rooms.get(i).getRoomid()+" "+rooms.get(i).getRoomtype()+" "+
                                    rooms.get(i).getTotal());
        }

        /*
        Room room  = test.findByRoomid("1012");
        System.out.println(room.getRoomid()+" "+room.getRoomtype()+" "+room.getTotal());
         */
        /*
        List<Room> rooms = test.findByRoomtype(1);
        for(int i = 0; i < rooms.size(); i++) {
            System.out.println(rooms.get(i).getRoomid()+" "+rooms.get(i).getRoomtype()+" "+
                                    rooms.get(i).getTotal());
        }
         */
    }
}
